var AjaxMethod = require("../getAjax.js"); 
var ajax = new  AjaxMethod;
export default{
    state:{
    	 curGoodId:'',
         curGoodNormId:'', // 当前货品规格id.切换货品时使用
         curHuoPinId:'',//添加购物车提交时需要的当前货品的ID；
         data_goodAllData:{},
         data_goodInfo:{},
         data_goodDefault:{},
         data_goodNorms:[],
         data_goodChange:{},
         data_goodDetail:{},
         data_goodEval:{},
         data_goodEvalVO:{},
         data_cart:{},
         data_goodNum:1,
         data_goodsList:{}, // 商品列表
    },
    mutations:{
        //商品 所有信息
        set_goodAllData:function(state,{list}){
            state.data_goodAllData = list;
        },
        // 商品详情 第一个tab
        set_goodInfo:function(state,{list}){
    	   state.data_goodInfo = list;
        },
        // 默认货品
        set_goodDefult:function(state,{list}){
            state.data_goodDefault = list;
        },
        //可选规格
        set_goodNorms:function(state,{list}){
            state.data_goodNorms = list;
        },
        //切换货品
        set_goodChange:function(state,{list}){
          state.data_goodChange = list;  
        },
        //set_goodDetail
        set_goodDetail:function(state,{list}){
            state.data_goodDetail = list;
        },
        // 商品用户评价  第三个tab
        set_goodEval:function(state,{list}){
            state.data_goodEval = list;
        },
        //用户操作信息 如 评价总数，评价率
        set_goodEvalVO:function(state,{list}){
          state.data_goodEvalVO = list;  
        },
        //添加购物车
        set_cart:function(state,{list}){
            state.data_cart = list;
        },
        //商品列表
        set_goodsList:function(state,{list}){
            state.data_goodsList  = list;
        }
        
    },
    actions:{
     	//商品
    	get_goodInfo:function(context){
            var _goodId = localMethod.prototype.get('goodId')
    		var Atype = "post", 
				//Aurl = baseUrl+'/products/'+_goodId+'.html',
                Aurl = baseUrl+"/products/"+_goodId+".html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{}}";
			ajax.getJson(Atype,Aurl,function(res){
				//console.log(res)
				//if(){}
                context.commit("set_goodAllData",{list:res.data}); // 
				context.commit("set_goodInfo",{list:res.data.product});// 
                context.commit("set_goodDefult",{list:res.data.goods});// 默认货品详情
                context.commit("set_goodNorms",{list:res.data.norms}); //商品可选规格
			},function(err){
				console.log(err)
			},function(){
				//context.state.Ajaxing = true; // 加载中显示...
			})
    	},
        //切换货品
        get_goodChange:function(context){
            var _goodId = localMethod.prototype.get('goodId');
            var _goodNormId = context.state.curGoodNormId;
            var Atype = "post", 
                //Aurl = baseUrl+'/products/'+_goodId+'.html',
                Aurl = baseUrl+"/products/getGoodsInfo.html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{productId:"+_goodId+",normAttrId:'"+_goodNormId+"'}}";
            ajax.getJson(Atype,Aurl,function(res){
                console.log(res)
                
                context.commit("set_goodDefult",{list:res.data.productGoods});
            },function(err){
                console.log(err)
            },function(){
                //context.state.Ajaxing = true; // 加载中显示...
            })
        },
        //详情 
        get_goodDetail:function(context){
            var _goodId = localMethod.prototype.get('goodId')
            var Atype = "post", 
                //Aurl = baseUrl+'/products/'+_goodId+'.html',
                Aurl = baseUrl+"/products/info/"+_goodId+".html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{}}";
            ajax.getJson(Atype,Aurl,function(res){
                //console.log(res)
                //if(){}
                context.commit("set_goodDetail",{list:res.data});
            },function(err){
                console.log(err)
            },function(){
                //context.state.Ajaxing = true; // 加载中显示...
            })
        },
    	// 商品 评价
        get_goodEval:function(context){
            var _goodId = localMethod.prototype.get('goodId')
            var Atype = "post", 
                //Aurl = baseUrl+'/products/'+_goodId+'.html',
                Aurl = baseUrl+"/products/comment/"+_goodId+".html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{}}";
            ajax.getJson(Atype,Aurl,function(res){
                //console.log(res)
                //if(){}
                context.commit("set_goodEval",{list:res.data});
                context.commit("set_goodEvalVO",{list:res.data.statisticsVO});//评价：用户操作信息 如   评价数，haopingl
            },function(err){
                console.log(err)
            },function(){
                //context.state.Ajaxing = true; // 加载中显示...
            })
        },
        //添加购物车
        get_cart:function(context){
            var _goodId = localMethod.prototype.get('goodId'); //商品ID
            var _goodNormId = context.state.curHuoPinId; // 货品ID
            var _goodNum = context.state.data_goodNum;
            var Atype = "post", 
                Aurl = baseUrl+"/carts/addtocart.html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{productId:'"+_goodId+"',productGoodId:'"+_goodNormId+"',number:"+_goodNum+"}}";
            ajax.getJson(Atype,Aurl,function(res){
                console.log(res)
                //if(){}
                context.commit("set_cart",{list:res});
            },function(err){
                console.log(err)
            },function(){
                //context.state.Ajaxing = true; // 加载中显示...
            })
        },
        //
        get_goodsList:function(context){
            var Atype = "post", 
                Aurl = baseUrl+"/cates/0-368-0-0-0-0-0-0-0-0.html?REQUEST_JSON={visitChannel:2,pageIndex:1,CURRENCY:'CNY',commandInfo:{}}";
            ajax.getJson(Atype,Aurl,function(res){
                console.log(res)
                //if(){}
                context.commit("set_goodsList",{list:res.data.producListVOs});
            },function(err){
                console.log(err)
            },function(){
                //context.state.Ajaxing = true; // 加载中显示...
            })
        }
    	
    }
}